package com.xiaoshuidi.cloud.module.rooms.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.equipmentStocktaking.vo.EquipmentStocktakingCreateReqVO;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.equipmentStocktaking.vo.EquipmentStocktakingPageReqVO;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.equipmentStocktaking.vo.EquipmentStocktakingPageResp;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.equipmentStocktaking.vo.EquipmentStocktakingSelectResp;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomequipment.vo.RoomEquipmentQueryPd;
import com.xiaoshuidi.cloud.module.rooms.convert.equipmentstocktaking.EquipmentStocktakingConvert;
import com.xiaoshuidi.cloud.module.rooms.framework.utils.CodeUtil;
import com.xiaoshuidi.cloud.module.rooms.mapper.EquipmentStocktakingMapper;
import com.xiaoshuidi.cloud.module.rooms.pojo.EquipmentStocktaking;
import com.xiaoshuidi.cloud.module.rooms.pojo.EquipmentStocktakingList;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomApartmentArea;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomHouse;
import com.xiaoshuidi.cloud.module.rooms.repository.RoomApartmentAreaRepository;
import com.xiaoshuidi.cloud.module.rooms.repository.RoomHouseRepository;
import com.xiaoshuidi.cloud.module.rooms.repository.irepository.IEquipmentStocktakingListRepository;
import com.xiaoshuidi.cloud.module.rooms.repository.irepository.IEquipmentStocktakingRepository;
import com.xiaoshuidi.cloud.module.rooms.repository.irepository.IRoomEquipmentRepository;
import com.xiaoshuidi.cloud.module.rooms.service.IEquipmentStocktakingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: Link
 * @CreateTime: 2023-05-21 17:14
 * @Version: 1.0
 */
@Slf4j
@Service
@Validated
@RequiredArgsConstructor
public class EquipmentStocktakingServiceImpl extends ServiceImpl<EquipmentStocktakingMapper, EquipmentStocktaking> implements IEquipmentStocktakingService {

    @Resource
    private IEquipmentStocktakingRepository iEquipmentStocktakingRepository;

    @Resource
    private IEquipmentStocktakingListRepository iEquipmentStocktakingListRepository;

    @Resource
    private IRoomEquipmentRepository roomEquipmentRepository;

    private final RoomApartmentAreaRepository roomApartmentAreaRepository;

    private final RoomHouseRepository roomHouseRepository;

    private final CodeUtil codeUtil;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long createEquipmentStocktakingList(EquipmentStocktakingCreateReqVO createReqVO) {

        EquipmentStocktaking equipmentStocktaking = EquipmentStocktakingConvert.INSTANCE.convert(createReqVO);
        equipmentStocktaking.setNo(codeUtil.nextCode("st"));
        equipmentStocktaking.setStatus("0");
        //存入盘点单表
        iEquipmentStocktakingRepository.save(equipmentStocktaking);
        log.info("存入盘点单表,成功...");
        log.info("准备构造，盘点清单表入参...");
        //存入盘点list表
        List<EquipmentStocktakingList> equipmentStocktakingListDoList = new ArrayList<>();
        Long roomStocktakingId = equipmentStocktaking.getId();
        List<RoomEquipmentQueryPd> roomEquipmentList = new ArrayList<>();
        roomEquipmentList = roomEquipmentRepository.queryPdList(createReqVO.getApartmentIds());
        log.info("查询到资产列表,成功，个数：" + roomEquipmentList.size());
        //盘点非公区 和公区
        //查询资产列表
        for (RoomEquipmentQueryPd re : roomEquipmentList) {
            EquipmentStocktakingList pdListDo = new EquipmentStocktakingList();
            pdListDo.setRoomStocktakingId(roomStocktakingId);   //盘点单ID
            pdListDo.setEquipmentCategoryId(re.getCategoryId());    //资产类别ID
            pdListDo.setEquipmentCategoryName(re.getName());    //资产名称
            pdListDo.setApartmentName(re.getApartmentName());
            pdListDo.setEquipmentId(re.getEquipmentId());
            pdListDo.setRoomId(re.getRoomId()); //关联的房间ID
            pdListDo.setIsPub(re.getIsPub());
            pdListDo.setEquipmentCategoryUnit(re.getUnit());//资产规格
            pdListDo.setEquipmentCode(re.getCode());
            pdListDo.setPositionType(re.getPositionType());
            pdListDo.setStatus(re.getStatus());
            pdListDo.setWarrantyMonth(re.getWarrantyTime());    //保修期
            pdListDo.setSupplierId(re.getSupplierId());
            pdListDo.setSupplierName(re.getSupplierName());
            pdListDo.setSupplierPhone(re.getSupplierPhone());
            pdListDo.setArea(re.getArea());
            pdListDo.setApartmentId(re.getApartmentId());
            pdListDo.setBuildingNo(re.getBuildingNo());
            pdListDo.setUnitNo(re.getUnitNo());
            pdListDo.setIsConcentrated(re.getIsConcentrated());
            pdListDo.setRoomHouseName(re.getRoomHouseName());
            pdListDo.setFloorNo(re.getFloorNo());
            equipmentStocktakingListDoList.add(pdListDo);
        }

        iEquipmentStocktakingListRepository.saveBatch(equipmentStocktakingListDoList);

        return roomStocktakingId;
    }

    @Override
    public PageResult<EquipmentStocktakingPageResp> getEquipmentStocktakingPage(EquipmentStocktakingPageReqVO reqVO) {
        PageResult<EquipmentStocktaking> list = iEquipmentStocktakingRepository.getEquipmentStocktakingPage(reqVO);
        List<EquipmentStocktakingPageResp> pageList = list.getList().stream().map(p -> {
            EquipmentStocktakingPageResp resp = new EquipmentStocktakingPageResp();
            BeanUtils.copyProperties(p, resp);
            resp.setPdTime(LocalDateTimeUtil.format(p.getPdTime(), "yyyy-MM-dd"));
            return resp;
        }).collect(Collectors.toList());
        PageResult<EquipmentStocktakingPageResp> page = PageResult.empty();
        page.setList(pageList);
        page.setTotal(list.getTotal());
        return page;
    }

    @Override
    public List<EquipmentStocktakingSelectResp> getXqList(List<Long> ids) {
        List<EquipmentStocktakingSelectResp> retList = new ArrayList<>();
        List<RoomApartmentArea> xqList = roomApartmentAreaRepository.getXqList(ids);

        for (RoomApartmentArea rr : xqList) {
            EquipmentStocktakingSelectResp es = new EquipmentStocktakingSelectResp();
            es.setId(rr.getId());
            es.setArea(rr.getArea());
            retList.add(es);
        }
        return retList;

    }

    @Override
    public Map<String, List<EquipmentStocktakingSelectResp>> getRoomList(List<Long> ids) {
        List<EquipmentStocktakingSelectResp> esList = new ArrayList<>();
        List<RoomHouse> searchList = roomHouseRepository.getRoomList(ids);
        for (RoomHouse rr : searchList) {
            EquipmentStocktakingSelectResp es = new EquipmentStocktakingSelectResp();
            es.setId(rr.getId());
            es.setName(rr.getName());
            es.setFloorId(rr.getFloorId());
            es.setFloorNo(rr.getFloorNo());
            esList.add(es);
        }

        Map<String, List<EquipmentStocktakingSelectResp>> collect = esList.stream().collect(
                Collectors.groupingBy(EquipmentStocktakingSelectResp::getFloorNo));
        return collect;

    }

}
